@extends('member.masterCommon')
@section('content')


    <div class="row">
        <div class="col-md-12  header-wrapper" >
            <h1 class="page-header">标签管理</h1>
            <p class="page-subtitle">网站文章、视频、图集的标签都在这里</p>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <ol class="breadcrumb">
        <li><a href="{{ route('tag.index') }}">标签管理</a></li>
        <li class="active">标签列表</li>
    </ol>
    <!-- /.row -->
    <div class="row">
        <div class="col-md-12">
            <div class="row">
                <p>
                    <button type="button" class="btn btn-primary btn-xs" onclick="window.open('{{ route('tag.create') }}','_self')"><i class="fa fa-plus"></i> 新增</button>
                    <button type="button" class="btn btn-success btn-xs checked_all"><i class="fa fa-arrows"></i> 全选</button>
                    <button type="button" class="btn btn-warning btn-xs"><i class="fa fa-pencil"></i> 审核</button>
                    <button type="button" class="btn btn-danger btn-xs"><i class="fa fa-times"></i> 停用</button>
                    <button type="button" class="btn btn-primary btn-xs" onclick="window.open('{{ route('tag.index') }}?is_rec=on','_self')"><i class="fa fa-plus"></i>显示已推荐主播</button>
                </p>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <form>
                        <label>搜索:<input type="search" class="form-control" placeholder="" name="s" id="search" ></label>
                    </form>
                </div>
            </div>
            <table class="table table-bordered table-hover" id="dataTables-userlist">
                <thead>
                <tr>
                    <th width="100"><input type="checkbox" class="checked_all" /> </th>
                    <th width="100">ID</th>
                    <th>标签名</th>
                    <th>文档数</th>
                    <th>权重</th>
                    <th>点击量</th>
                    <th>权限</th>
                    @if($is_anchor)
                        <th>是否在线</th>
                    @endif
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($tags as $tag)
                    <tr>
                        <td><input type="checkbox" /></td>
                        <td>{{$tag->id}}</td>
                        <td>
                            @if($tag->logo)
                                <img src="{{route('image', [$tag->logo, '30x30'])}}" onerror="$(this).remove()" alt="" class="gridpic">
                            @endif
                            {{$tag->name}}
                        </td>
                        <td>{{$tag->archives()->count()}}</td>
                        <td>{{(int)$tag->weight}}</td>
                        <td>{{$tag->visit_count}}</td>
                        <td class="center tag-status">
                            @if($tag->status == 1)
                                <span class="status inactive">待审核标签</span>
                            @elseif($tag->status == 2)
                                <span class="status active">开放浏览</span>
                            @elseif($tag->status == 4)
                                <span class="status inactive">已停用</span>
                            @endif
                        </td>
                        @if ($is_anchor)
                            <td class="center">
                                @if ($tag->anchor->status === 1)
                                    <span class="status active">主播在线</span>
                                @else
                                    <span class="status inactive">主播不在线</span>
                                @endif
                            </td>
                        @endif
                        <td class="center">
                            <a href="{{route('tag.edit', [$tag->id])}}" class="btn btn-circle btn-primary ">编辑</a>
                            <a href="{{ $tag->url }}" class="btn btn-circle btn-success ">预览</a>
                            @if($tag->status == 2)
                                <a data-href="{{route('tag.status', [$tag->pinyin, 4])}}" data-pinyin="{{$tag->pinyin}}" class="ajax-request btn btn-circle btn-danger ">停用</a>
                            @else
                                <a data-href="{{route('tag.status', [$tag->pinyin, 2])}}" data-pinyin="{{$tag->pinyin}}" class="ajax-request btn btn-circle btn-success">启用</a>
                            @endif
                            @if($tag->anchor_id)
                                @if($tag->is_rec)
                                    <a data-href="{{ route('anchors.unrec', [$tag->anchor->id]) }}"  class="btn btn-circle btn-danger ajax-rec">取推</a>
                                @else
                                    <a data-href="{{ route('anchors.rec', [$tag->anchor->id]) }}" class="btn btn-circle btn-success ajax-rec">推荐</a>
                                @endif
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <div class="row"><div class="col-sm-6"><div class="dataTables_info" id="dataTables-userlist_info" role="status" aria-live="polite">每页显示20条数据，一共{{ $tags->total() }}数据</div></div>
                <div class="col-sm-6">
                    <div class="dataTables_paginate paging_full_numbers" id="dataTables-userlist_paginate">
                        {{$tags->links()}}
                    </div>
                </div>
            </div>
            <script>

            </script>
        </div>
    </div>
    <!-- /.row -->
@endsection

@section('scripts')
    <!-- Custom Theme JavaScript -->
    <script>

        var $status_4 = [
            'btn-danger', 'btn-success', '停用',  4,
            '<span class="status active">开放浏览</span>'
        ];
        var $status_2 = [
            'btn-success', 'btn-danger', '启用', 2,
            '<span class="status inactive">已停用</span>'
        ];
        $('.ajax-request').on('click', function () {
            var $this = $(this)
            $.getJSON($this.data('href'), function (response) {
                var node = $this.hasClass('btn-success') ? $status_4 : $status_2
                $this[0].dataset.href = "/member/tag/"+$this[0].dataset.pinyin+"/status/" + node[3]
                $this
                        .addClass(node[0])
                        .removeClass(node[1])
                        .text(node[2]).parents('tr')
                        .find('.tag-status')
                        .html(node[4])
                layer.msg(response.msg, {icon: 1})
            })
        });

        $('.ajax-rec').on('click', function () {
            var $this = $(this);
           $.getJSON($this.data('href'), function (res) {
               layer.msg(res.msg, {icon: 1})

               if (res.msg.match("取消")) {
                   $this.data("href", $this.data('href').replace(/\/unrec/, '/rec'));
                   $this.removeClass('btn-danger').addClass('btn-success').text('推荐');
               } else {
                   $this.data("href", $this.data('href').replace(/\/rec/, '/unrec'));
                   $this.addClass('btn-danger').removeClass('btn-success').text('取推');
               }
           })
        });

        $('input[type=checkbox]').on('click', function (event) {
            if ($(event.target).hasClass('checked_all')) return ;
            $('.checked_all').prop('checked', false)}
        );

        $('.checked_all').on('click', function (event) {

            if (event.target.tagName != 'INPUT') {
                $(this).prop('checked', !$(this).prop('checked'))
            }

            $('input[type=checkbox]').prop('checked', $(this).prop('checked'))
        })
    </script>
@endsection